var util = require('util');

app.get('/device', function(req, res) {
	res.local('title', 'Devices');
	model.Device.find({}).populate('ports').run(function(e, devices) {
		res.local('devices', devices);
		res.render('Device/list.jade', {
			pretty : true
		});
	});
});

app.get('/device/:id', function(req, res) {
	res.local('title', 'Device');
	res.local('scripts', {
		'updatePlugin' : '/static/script/pluggin/update.js'
	});
	model.Device.findById(req.params.id).populate('ports').run(
			function(e, device) {
				if (e)
					log.error(e, 'Controls/Device.js:17');
				if (device) {
					log.debug(util.inspect(device));
					res.local('device', device);
					res.render('Device/view.jade', {
						pretty : true
					});
				} else {
					res.redirect('https://' + req.header('X-Request-Host')
							+ '/device/');
				}
			});
});

app.get('/device/:id/edit', function(req, res) {
	res.local('title', 'Edit Device');
	model.Device.findById(req.params.id).populate('ports').run(
			function(e, device) {
				if (device) {
					res.local('action', 'update');
					res.local('device', device);
					res.render('Device/edit.jade', {
						pretty : true
					});
				} else {
					res.redirect('https://' + req.header('X-Request-Host')
							+ '/device/');
				}
			});
});

app.get('/device/:id/add', function(req, res) {
	res.local('title', 'Add Device');
	model.Device.findOne({
		hwAddress : req.params.id
	}, function(e, device) {
		if (device) {
			res.local('action', 'add');
			res.local('device', device);
			res.render('Device/edit.jade', {
				pretty : true
			});
		} else {
			res
					.redirect('https://' + req.header('X-Request-Host')
							+ '/device/');
		}
	});
});

app.get('/device/:id/remove', function(req, res) {
	res.local('title', 'Add Device');
	model.Device.remove({
		_id : req.params.id
	}, function(e) {
		if (e) {
			log.error(e, "Controls/Device:71");
			res
					.redirect('https://' + req.header('X-Request-Host')
							+ '/device/');
		}
	});
});

app.post('/device', function(req, res) {
	if (typeof (req.body.action) != 'undefined') {
		switch (req.body.action) {
		case 'add':
			var d = new model.Device();
			d.name = req.body.name;
			d.hwAddress = req.body.hwAddress;
			d.inetAddress = req.body.inetAddress;
			d.location = req.body.location;
			d.description = req.body.description;
			d.status = 'offline';

			model.Device.remove({
				hwAddress : req.body.hwAddress
			}, function(e) {
				d.save(function(e) {
					if (e) {
						log.error(e, 'Control/Device:82');
						res.redirect('https://' + req.header('X-Request-Host')
								+ '/device/');
					} else {
						res.redirect('https://' + req.header('X-Request-Host')
								+ '/device/' + d._id);
					}
				});
			});
			break;
		case 'update':
			model.Device.update({
				_id : req.body._id
			}, {
				name : req.body.name,
				location : req.body.location,
				description : req.body.description,
				status : 'offline'
			}, function(e) {
				if (e) {
					log.error(e, 'Control/Device:102');
					res.redirect('https://' + req.header('X-Request-Host')
							+ '/device/');
				} else {
					res.redirect('https://' + req.header('X-Request-Host')
							+ '/device/' + req.body._id);
				}
			});
			break;
		default:
			res
					.redirect('https://' + req.header('X-Request-Host')
							+ '/device/');
		}
	}
});
